package com.cskaoyan.service;

import com.cskaoyan.bean.MarketAddress;
import com.cskaoyan.bean.MarketAddressExample;
import com.cskaoyan.bean.MarketUser;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketAddressMapper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.tomcat.util.security.Escape;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author gatsby
 * @since 2022/10/14 15:23
 */
@Service
public class WxAddressServiceImpl implements WxAddressService {

    @Autowired
    MarketAddressMapper marketAddressMapper;

    /**
     * 查询用户全部收货地址的业务逻辑
     *
     * @param id
     * @return com.cskaoyan.bean.common.CommonData<com.cskaoyan.bean.MarketAddress>
     * @author gatsby
     * @since 2022/10/14 15:30
     */
    @Override
    public CommonData<MarketAddress> queryAllAddressByUserId(Integer id) {

        MarketAddressExample example = new MarketAddressExample();
        example.setDistinct(true);
        MarketAddressExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(id);
        criteria.andDeletedEqualTo(false);
        List<MarketAddress> marketAddressList = marketAddressMapper.selectByExample(example);
        PageInfo<MarketAddress> pageInfo = new PageInfo<>(marketAddressList);
        return CommonData.data(pageInfo);
    }

    /**
     * 显示地址详情的业务逻辑
     *
     * @param id
     * @return com.cskaoyan.bean.MarketAddress
     * @author gatsby
     * @since 2022/10/14 15:58
     */
    @Override
    public MarketAddress queryAddressById(Integer id) {

        MarketAddressExample example = new MarketAddressExample();
        example.setDistinct(true);
        MarketAddressExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(id);
        criteria.andDeletedEqualTo(false);
        //MarketAddress marketAddress = marketAddressMapper.selectByPrimaryKey(id);
        List<MarketAddress> marketAddresses = marketAddressMapper.selectByExample(example);

        return marketAddresses.get(0);
    }

    /**
     * 保存地址的业务逻辑
     *
     * @param marketAddress
     */
    @Override
    public int saveAddress(MarketAddress marketAddress) {

        //先判断是否要将当前这个地址设为默认地址
        if(marketAddress.getIsDefault()==true){
            //将原来为默认值的地址改为不是默认地址
            MarketAddressExample example = new MarketAddressExample();
            example.setDistinct(true);
            MarketAddressExample.Criteria criteria = example.createCriteria();
            criteria.andIsDefaultEqualTo(true);
            criteria.andDeletedEqualTo(false);
            MarketAddress marketAddress1 = new MarketAddress();
            marketAddress1.setIsDefault(false);
            marketAddressMapper.updateByExampleSelective(marketAddress1,example);
        }

        if (marketAddress.getId() != 0) {
            //更新的业务逻辑

            marketAddressMapper.updateByPrimaryKeySelective(marketAddress);
            return  marketAddress.getId();
        }
        //新建的业务逻辑
        marketAddress.setId(null);
        marketAddress.setDeleted(false);
        MarketUser marketUser = (MarketUser)SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();
        marketAddress.setUserId(marketUser.getId());
        marketAddress.setAddTime(new Date());
        marketAddress.setUpdateTime(new Date());

        marketAddressMapper.insertSelective(marketAddress);
        return marketAddress.getId();

    }

    /**
     * 删除地址的业务逻辑
     *
     * @param id
     * @return void
     * @author gatsby
     * @since 2022/10/14 16:15
     */
    @Override
    public void delete(Integer id) {
        marketAddressMapper.updateDeletedToTrueByPrimaryKey(id);
    }
}
